<template>
	<view class="content">
		<listHead :only="only" :title='written'></listHead>
		<view class="bottom bottom-ewm">
			{{nfcId}}
		</view>
		<view class="top">
			<view class="top-top" v-for="(item,index) in list" :key="index">
				<view class="top-left" v-if="item.title.length>2">{{item.title}}</view>
				<view class="top-left " v-else>
					<view class="top-title">
						{{item.title}}
					</view>
				</view>
				<view class="top-right">{{item.name}}</view>
			</view>
		</view>
		<view class="top-bottom">
			绑定结果
		</view>
		<view class="bottom" style="color: red;">
			{{listValue}}
		</view>
		<view class="bottom-bottom" style="padding: 0 20px;">
			<u-button type="primary" class="btn1" text="重新绑定" :color="listValue !='' ?'#FF6E99':'#CACACA'"
				@tap="cancel"></u-button>
		</view>
		<scan-code @scanCodeId="scanCodeId" value="仪器绑定"></scan-code>
	</view>

</template>

<script>
	import listHead from '../../components/html/listHead.vue'
	import {
		BindEquipBed,
		ScanRFID,
		ScanPatientinfo
	} from "@/api/user.js"
	import {
		mapMutations,
		mapState
	} from "vuex";
	import scanCode from "../../components/html/scan-code.vue"
	export default {
		components: {
			listHead,
			scanCode
		},
		data() {
			return {
				only: false,
				written: '仪器绑定',
				show: true,
				list: [{
						title: '床位号',
						name: ''
					},
					{
						title: '病历号',
						name: ''
					},
					{
						title: '姓名',
						name: ''
					},
					{
						title: '年龄',
						name: ''
					},
					{
						title: '性别',
						name: ''
					},
				],
				nfcId: '',
				lsii: [],
				listValue: '',
				time: 0
			}
		},
		onLoad(opion) {
		   if(opion){
			   this.nfcId = opion.id
		   }
			
			// var _this = this
			// uni.$on('scancodedate', function(content) {
			// 	const add = content.split('|')
			// 	_this.content = add
			// })
		},
		watch: {
			nfcId: {
				handler(newval, oldval) {
					if (String(newval).startsWith("A1") ||String(newval).length==12) {
						if (newval != oldval) {
							this.list = [{
									title: '床位号',
									name: ''
								},
								{
									title: '病历号',
									name: ''
								},
								{
									title: '姓名',
									name: ''
								},
								{
									title: '年龄',
									name: ''
								},
								{
									title: '性别',
									name: ''
								},
							]
							this.listValue = ''
							this.time = 0
							this.lsii = []
						}
					}
				}
			}
		},
		computed: {
			...mapState('user', ['userInfo', 'passObj'])
		},
		methods: {
			nav() {},
			cancel() {
				this.list = [{
						title: '床位号',
						name: ''
					},
					{
						title: '病历号',
						name: ''
					},
					{
						title: '姓名',
						name: ''
					},
					{
						title: '年龄',
						name: ''
					},
					{
						title: '性别',
						name: ''
					}
				]
				this.listValue = ''
				this.nfcId = ''
				this.lsii = []
				this.time = 0
			},
			async medicalRecord(id) {
				if (this.lsii.length == 0) {
					const user = uni.getStorageSync('userInfo')
					const data = {
						HosKind: user.hoscode,
						mrn: id
					}
					const res = await this.$http.ScanPatientinfo(data)
					return res
				}
			},
			async scanCodeId(id, value) {
				  console.log(id,value);
				let idCode = Number(id) % 1 === 0
				if (idCode && String(id).length < 12) {
					let res1 = await this.medicalRecord(id)

					if (res1[0].result == '1') {
						this.lsii = res1
						this.list = [{
								title: '床位号',
								name: res1[0].bedid
							},
							{
								title: '病历号',
								name: res1[0].mrn
							},
							{
								title: '姓名',
								name: res1[0].pname
							},
							{
								title: '年龄',
								name: `${res1[0].age}${res1[0].ageunit == 'Y'?'岁':''}`
							},
							{
								title: '性别',
								name: res1[0].psex == 'M' ? '男' : '女'
							}
						]
					} else {
						this.lsii = []
						this.list = [{
								title: '床位号',
								name: ''
							},
							{
								title: '病历号',
								name: ''
							},
							{
								title: '姓名',
								name: ''
							},
							{
								title: '年龄',
								name: ''
							},
							{
								title: '性别',
								name: ''
							}
						]
					}

				} else if (String(id).startsWith('A1')||String(id).length==12) {
					this.nfcId = id
				}
				if (this.lsii[0]?.mrn && this.nfcId) {
					this.LISTAES(this.lsii, this.nfcId)
				}
			},
			async LISTAES(res, id) {
				console.log(res,id);
				
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					series: res[0]?.series ? res[0].series : '',
					sn: id,
					ward: user.HosKind,
					bed: res[0]?.bedid ? res[0].bedid : '',
					patientZyh: res[0]?.mrn ? res[0].mrn : '',
					patientName: res[0]?.pname ? res[0].pname : '',
				}
				console.log(res,id,'res,id')
				
				const res2 = await this.$http.BindEquipBed(data)
				console.log(res,'res')
				if (res2 == undefined) {
					return
				}
				if ((typeof res2 == 'string') && res2.constructor == String && this.time > 1) {
					this.listValue = '绑定失败！' + res2
					return
				}
				if (res2[0].result == '1') {
					this.listValue = '绑定成功'
				} else {
					if (!res2[0]?.result) {
						this.listValue = res2
						return
					} else {
						uni.showToast({
							title: res2[0].result, //显示的文字
							duration: 1500, //显示多少时间，默认1500毫秒
							icon: "none", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
							mask: true
						})
						this.listValue = res2[0].result
					}
				}
			}
		},
		
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100vh;
		overflow: hidden;
	}

	.top {
		padding: 20px 20px 0;
	}

	.top-top {
		display: flex;
		align-items: center;
		margin-bottom: 10px;

		.top-left {
			width: 22%;
			height: 36px;
			background: #B9CACB;
			border-radius: 6px 2px 2px 6px;
			font-size: 16px;
			font-family: SourceHanSansCN-Medium, SourceHanSansCN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 24px;
			display: flex;
			align-items: center;
			justify-content: center;
			border-right: 0px;
		}

		.top-title {
			width: 48px;
			text-align-last: justify;
			text-align: justify;
		}

		.top-right {
			width: 78%;
			height: 34px;
			background: #FFFFFF;
			border-radius: 0px 6px 6px 0px;
			border: 1px solid #B9CACB;
			border-left: 0px;
			display: flex;
			align-items: center;
			padding: 0 20rpx;
		}

	}

	.top-bottom {
		height: 50px;
		background: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 21px;
		font-family: SourceHanSansCN-Normal, SourceHanSansCN;
		font-weight: 400;
		color: #333333;
		line-height: 32px;
		margin-top: 20px;
		box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.1);
		border-top: 1px solid rgba(0, 0, 0, 0.04);
	}

	.bottom {
		height: calc(100% - 530px);
		background: #F8F8F8;
		overflow-y: scroll;
	
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 24px;
	}

	.bottom-bottom {
		padding: 0 20px;
		margin-top: 20px;
	}

	.u-reset-button ::v-deep .u-button__text {
		font-size: 22px !important;
		font-family: SourceHanSansCN-Medium, SourceHanSansCN;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 33px;
	}

	.btn1 {
		box-shadow: 0px 2px 6px 0px #CACACA;
		height: 56px;
	}

	.bottom-ewm {
		display: flex;
		justify-content: center;
		width: 100%;
		text-align: center;
		justify-content: center;
		align-items: center;
		padding-top: 10px;
		height: 54px !important;
		font-size: 18px;
	}
</style>
